package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.jiayen.util.DisplayUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.util.L;

/**
 * Created by jiayen on 17/5/26.
 */

public class InfoDetailSelectDialog  extends Dialog{

    private DelResonDialog.DelDone delDone;
    private Context context;
    private Window dialogWindow;
    private WindowManager.LayoutParams lp;
    private int screenHgith;
    private int dialogHeigth;//对话框高度  这里默认设为120dp
    private int dialogWidth;//对话框宽度  这里默认设为180dp
    private int screenWidth;//屏幕宽带
    private View mView;

    private View.OnClickListener  onItemClick;

    public InfoDetailSelectDialog(Context context) {
        this(context,0);
        dialogWidth= DisplayUtil.dip2px(context,80);
    }

    public void setOnItemClick(View.OnClickListener onItemClick) {
        this.onItemClick = onItemClick;
    }

    public InfoDetailSelectDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context =context;
    }

    protected InfoDetailSelectDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context=context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mView = LayoutInflater.from(context).inflate(R.layout.info_detail_select_dialog,null);//第一次先初始化
        setContentView(mView);
        init();
        dialogWindow = getWindow(); //得到对话框
        dialogWindow.setWindowAnimations(R.style.dialog_anim); //设置窗口弹出动画
    }

    public void show(int x, int y) {
        L.e("show","======x="+x+"=======y="+y);
        windowDeploy(x, y);
        setCanceledOnTouchOutside(true);
        show();
    }

    //设置窗口显示
    private void windowDeploy(int x, int y) {
        dialogWindow = getWindow(); //得到对话框
        dialogWindow.setGravity(Gravity.LEFT|Gravity.TOP);

        dialogWindow.setBackgroundDrawableResource(R.color.translate); //设置对话框背景为透明
        WindowManager.LayoutParams wl = dialogWindow.getAttributes();
        //根据x，y坐标设置窗口需要显示的位置
        wl.x=x-dialogWidth;

            wl.y = y; //y小于0上移，大于0下移
        dialogWindow.setAttributes(wl);
    }

    private void init() {
        if(onItemClick!=null){
            findViewById(R.id.set_font).setOnClickListener(onItemClick);
            findViewById(R.id.to_feekback).setOnClickListener(onItemClick);
        }
    }
}
